package com.example.admin.brainwave.ui.activity;


import android.Manifest;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.admin.brainwave.R;
import com.example.admin.brainwave.base.BaseActivity;
import com.example.admin.brainwave.bean.GetTesteeInfoBean;
import com.example.admin.brainwave.net.GetImageTask;
import com.example.admin.brainwave.net.GetTesteeInfoTask;
import com.example.admin.brainwave.utils.BitmapUtil;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.SPHelper;
import com.example.admin.brainwave.utils.ToastUtil;
import com.example.admin.brainwave.utils.permission.PermissionUtil;

import butterknife.BindView;

public class MainActivity extends BaseActivity {


    @BindView(R.id.iv_top_back)
    ImageView ivTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.iv_top_login_out)
    ImageView ivTopLoginOut;
    @BindView(R.id.iv_main_head)
    ImageView ivMainHead;
    @BindView(R.id.tv_main_name)
    TextView tvMainName;
    @BindView(R.id.iv_main_language)
    ImageView ivMainLanguage;
    @BindView(R.id.tv_main_language)
    TextView tvMainLanguage;
    @BindView(R.id.iv_main_braincollection)
    ImageView ivMainBraincollection;
    @BindView(R.id.rl_main_braincollection)
    RelativeLayout rlMainBraincollection;
    @BindView(R.id.iv_main_history)
    ImageView ivMainHistory;
    @BindView(R.id.rl_main_history)
    RelativeLayout rlMainHistory;
    @BindView(R.id.iv_main_personal)
    ImageView ivMainPersonal;
    @BindView(R.id.rl_main_personal)
    RelativeLayout rlMainPersonal;
    @BindView(R.id.iv_main_account)
    ImageView ivMainAccount;
    @BindView(R.id.rl_main_account)
    RelativeLayout rlMainAccount;
    @BindView(R.id.rl_main_language)
    RelativeLayout rlMainLanguage;
    private static final int CODE_RES = 0X00001;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_main;
    }


    @Override
    protected void initView() {
        ivTopBack.setVisibility(View.GONE);
        ivTopLoginOut.setVisibility(View.VISIBLE);
        tvTopTitle.setText(getResources().getString(R.string.app_name));
        PermissionUtil.handleRunTimePermis(this,
                () -> {
                },
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.CAMERA,
                Manifest.permission.BLUETOOTH_ADMIN);
    }


    @Override
    protected void initData() {
        new GetTesteeInfoTask(this, o -> {
            if (o != null) {
                GetTesteeInfoBean info = (GetTesteeInfoBean) o;
                if (Const.OKCODE.equals(info.getCode())) {
                    showInfo(info);
                } else {
                    ToastUtil.showShort(MainActivity.this, info.getMessage());
                }
            }
        }).execute();
        new GetImageTask(this, o -> {
            if (o != null) {
                BitmapUtil.loadBitmap((String) o, MainActivity.this, ivMainHead);
            }
        }).execute();
    }

    private void showInfo(GetTesteeInfoBean info) {
        if (!TextUtils.isEmpty(info.getResult().getFullName())) {
            tvMainName.setText(getResources().getString(R.string.hello) + "，" + info.getResult().getFullName());
        } else {
            Intent intent = new Intent(MainActivity.this, PersonalDataActivity.class);
            startActivityForResult(intent, CODE_RES);
        }
    }

    @Override
    protected void addListener() {
        ivTopLoginOut.setOnClickListener(view -> {
            SPHelper.getInstance(MainActivity.this).putString("token", "");
            SPHelper.getInstance(MainActivity.this).putString("userId", "");
            finish();
        });
        rlMainLanguage.setOnClickListener(view -> startActivity(new Intent(MainActivity.this, SetLanguageActivity.class)));
        rlMainBraincollection.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, BrainCollectionHomeActivity.class)));
        rlMainHistory.setOnClickListener(view -> startActivity(new Intent(MainActivity.this, HistoryActivity.class)));
        rlMainPersonal.setOnClickListener((View v) -> {
            Intent intent = new Intent(MainActivity.this, PersonalDataActivity.class);
            startActivityForResult(intent, CODE_RES);
        });
        rlMainAccount.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, ResetPassActivity.class)));
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == CODE_RES) {
                initData();
            }
        }
    }
}
